package com.fly.lambda.service;

import com.fly.lambda.dao.FileDAO;
import com.fly.lambda.domain.FileInfo;
import com.fly.lambda.framework.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * Created by Fly on 2016/7/28.
 */
@Service
public class FileService {
    @Autowired
    FileDAO fileDAO;

    /**
     * 文件信息保存
     * @param fileInfo
     */
    public void saveFile(FileInfo fileInfo){
        if (StringUtils.isBlank(fileInfo.getFileId())){
            fileInfo.setFileId(CommonUtils.genPK());
            fileDAO.create(fileInfo);
        }
        else{
            fileDAO.update(fileInfo);
        }
    }

    /**
     * 删除文件信息及文件
     * @param fileId
     */
    public void deleteFile(String fileId){
        FileInfo file = fileDAO.getById(fileId);
        if (null != file){
            File f = new File(file.getFilePath());
            if (f.exists()){
                f.delete();
            }
            fileDAO.deleteById(fileId);
        }
    }
}
